const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const formatTime_ymd = date => {
    date = new Date(date);
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('.')
}

const countTime = (time) => {
    let leftTime = new Date(time);//设置截止时间                            
    let d, h, m, s, ms;
    if (leftTime >= 0) {
        d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
        h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
        m = Math.floor(leftTime / 1000 / 60 % 60);
        s = Math.floor(leftTime / 1000 % 60);
        ms = Math.floor(leftTime % 1000);
        if (ms < 100) {
            ms = "0" + ms;
        }
        if (s < 10) {
            s = "0" + s;
        }
        if (m < 10) {
            m = "0" + m;
        }
        if (h < 10) {
            h = "0" + h;
        }
        return {
            d,//#endregion
            h,//#endregion
            m,//#endregion
            s,//#endregion
            ms: ms.toString().slice(0, 1),
            timeout: false
        }
    } else {
        return {
            d: 0,//#endregion
            h: 0,//#endregion
            m: 0,//#endregion
            s: 0,//#endregion
            ms: 0,
            timeout: true
        }
    }
    //递归每秒调用countTime方法，显示动态时间效果
}

const getParams = (search, type1, type2) => {
    if (!search) {
        return {}
    }
    let paramsArr = search.split(type1),
        paramsObj = {};
    paramsArr.map((item) => {
        let params = item.split(type2)
        paramsObj[params[0]] = decodeURIComponent(params[1])
        return item
    })
    return paramsObj
}

//节流函数
const throttle = (fn, delay) => {
    let flag = true;
    return (...args) => {
        if (!flag) return;
        flag = false;
        setTimeout(() => {
            fn.apply(this, args);
            flag = true;
        }, delay)
    }
}
// 格式化时间日期
const yyyymmrr = () => {
    let timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    //获取当前时间
    let n = timestamp * 1000;
    let data = new Date(n);
    //年
    let year = data.getFullYear();
    //月
    let month = (data.getMonth() + 1 < 10 ? '0' + (data.getMonth() + 1) : data.getMonth() + 1);
    //日
    let day = data.getDate() < 10 ? '0' + data.getDate() : data.getDate();
    return [year, month, day].map(formatNumber).join('')
}


// 直传到oss
const upload = (params, filePath, dir = 'vipapply') => {
    let fileName = '';
    // 区分一下开发工具和手机真实环境
    if (filePath.indexOf('wxfile://') != -1) {
        fileName = filePath.replace('wxfile://', '');
    } else {
        let filePathSplit = filePath.split('.');
        fileName = filePathSplit[2] + '.' + filePathSplit[3];
    }
    let host = 'https://cdn.cailu88.com/';
    let key = `kingdom/user/${dir}/${yyyymmrr()}/${fileName}`;
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: host,
            filePath: filePath,
            name: 'file',
            formData: {
                'key': key,
                'policy': params.data.data.policy,
                'OSSAccessKeyId': params.data.data.accessId,
                'signature': params.data.data.signature,
                'success_action_status': '200',
            },
            success: (res) => {
                resolve(host + key);
            },
            fail: (err) => {
                reject(err);
            }
        })
    })
}

module.exports = {
    formatTime: formatTime,
    formatTime_ymd: formatTime_ymd,
    getParams: getParams,
    throttle: throttle,
    upload: upload,
    countTime: countTime
}